package com.talent.jiwen.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.talent.jiwen.ActivityManager;
import com.talent.jiwen.IndexActivity;
import com.talent.jiwen.R;
import com.talent.jiwen.my.MyFragment;
import com.talent.jiwen.my.PayActivity;
import com.talent.jiwen.my.RechargeActivity;
import com.talent.jiwen.teacher.CourseAllListFragment;
import com.talent.jiwen.teacher.CourseFragment;
import com.talent.jiwen.teacher.CourseWaitListFragment;
import com.talent.jiwen.util.Constant;
import com.talent.jiwen.util.LogUtil;
import com.talent.jiwen.util.SPConstant;
import com.talent.jiwen.util.SPUtil;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Constant.APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {
	}

	@Override
	public void onResp(BaseResp resp) {
//		Log.d(TAG, "onPayFinish, errCode = " + resp.errCode);
		LogUtil.e(TAG, "onPayFinish, errCode = " + resp.errCode);

//		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
//			AlertDialog.Builder builder = new AlertDialog.Builder(this);
//			builder.setTitle(R.string.app_tip);
//			builder.setMessage(getString(R.string.pay_result_callback_msg, String.valueOf(resp.errCode)));
//			builder.show();
//		}

		if(resp.getType()==ConstantsAPI.COMMAND_PAY_BY_WX){
			if(resp.errCode==0){

				switch (Constant.PAY_SCENE_FOR_WX){
					case Constant.WX_PAY_FOR_DEPOSIT:
						EventBus.getDefault().post(CourseAllListFragment.REFRESH_COURSE_ALL_LIST);
						break;
					case Constant.WX_PAY_FOR_RECHARGE:
//						SPUtil.stringIn(SPConstant.TOTAL_AMOUNT , RechargeActivity.rechargeMoney + "");
						EventBus.getDefault().post(MyFragment.REFRESH_DATA);
						break;
					case Constant.WX_PAY_FOR_ORDER:
						orderPayResult();
						break;
					default:
						orderPayResult();
						break;
				}
			}
			else {
				Toast.makeText(this, "支付失败", Toast.LENGTH_LONG).show();
			}
			finish();
		}
	}


	private void orderPayResult(){
		Toast.makeText(this, "支付成功", Toast.LENGTH_LONG).show();
		ActivityManager.getAppManager().finishActivity(PayActivity.class);

		Intent intent = new Intent(this, IndexActivity.class);
		IndexActivity.tab2checked = true;
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
		CourseFragment.fragment3Checked = true;
		EventBus.getDefault().post(CourseWaitListFragment.REFRESH_COURSE_WAIT_LIST);
		EventBus.getDefault().post(CourseAllListFragment.REFRESH_COURSE_ALL_LIST);
	}
}